package com.jogger.newprint.base;

/**
 * Created by it on 2017/2/21.
 */

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;

import com.jogger.newprint.utils.StatusUtil;
import com.jogger.newprint.utils.ToastUtil;

import java.io.Serializable;

import butterknife.ButterKnife;


/**
 * activity基类
 */

public abstract class BaseActivity extends AppCompatActivity {
    private boolean hideNavigation = false;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        //开启沉浸式状态栏
        StatusUtil statusUtil = new StatusUtil();
        if (isOpenStatus()) {
            statusUtil.openStatusActivity(this);
        }
        if (isFullScreen()) {
            statusUtil.setActivityFullScreen(this);
        }
        if (isHideNavigation()) {
            statusUtil.setActivityHideNavigation(this);
        }

        init();
        loadData();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    protected void init() {
    }

    protected void loadData() {
    }

    public abstract int getLayoutId();

    /**
     * 是否开启沉浸式状态栏
     */
    public boolean isOpenStatus() {
        return true;
    }

    /**
     * 底部导航条
     *
     * @return
     */
    public boolean isHideNavigation() {
        return hideNavigation;
    }

    public void setIsHideNavigation(boolean hideNavigation) {
        this.hideNavigation = hideNavigation;
    }

    /**
     * 是否全屏
     */
    public boolean isFullScreen() {
        return false;
    }

    public void startNewActivity(Class cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }

    public interface OnRequestPermissionListener {
        void onCompleted(boolean state);
    }

    private OnRequestPermissionListener onPermissionListener;

    /**
     * 请求单个权限
     *
     * @param permission
     * @param listener
     */
    public void requestPermission(String permission, OnRequestPermissionListener listener) {
        onPermissionListener = listener;
        int checkSelfPermission;
        try {
            checkSelfPermission = ActivityCompat.checkSelfPermission(this, permission);
        } catch (RuntimeException e) {
            ToastUtil.showToast(this, "请开启权限");
            return;
        }

        if (checkSelfPermission != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
                //权限已有
                if (onPermissionListener != null) {
                    onPermissionListener.onCompleted(true);
                }
            } else {
                ActivityCompat.requestPermissions(this, new String[]{permission}, 1);
            }

        } else {
            if (onPermissionListener != null) {
                onPermissionListener.onCompleted(true);
            }
        }
    }

    public void requestPermission(String[] permission, OnRequestPermissionListener listener) {
        onPermissionListener = listener;
        int checkSelfPermission;
        try {
            checkSelfPermission = ActivityCompat.checkSelfPermission(this, permission[1]);
        } catch (RuntimeException e) {
            ToastUtil.showToast(this, "请开启权限");
            return;
        }
        if (checkSelfPermission != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission[1])) {
//权限已有
                if (onPermissionListener != null) {
                    onPermissionListener.onCompleted(true);
                }
            } else {
                ActivityCompat.requestPermissions(this, permission, 1);
            }

        } else {
            if (onPermissionListener != null) {
                onPermissionListener.onCompleted(true);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        if (requestCode == 1 && permissions.length != 0) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //权限通过
                if (onPermissionListener != null) {
                    onPermissionListener.onCompleted(true);
                }
            } else {
                //权限拒绝
                if (onPermissionListener != null) {
                    onPermissionListener.onCompleted(false);
                }
            }
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    public void startNewActivity(Class cls, String key, Object value) {
        Intent intent = new Intent(this, cls);
        if (value instanceof Integer) {
            intent.putExtra(key, (Integer) value);
        }
        if (value instanceof Integer[]) {
            intent.putExtra(key, (Integer[]) value);
        }
        if (value instanceof String) {
            intent.putExtra(key, (String) value);
        }
        if (value instanceof String[]) {
            intent.putExtra(key, (String[]) value);
        }
        if (value instanceof Boolean) {
            intent.putExtra(key, (Boolean) value);
        }
        if (value instanceof Byte) {
            intent.putExtra(key, (Byte) value);
        }
        if (value instanceof Byte[]) {
            intent.putExtra(key, (Byte[]) value);
        }
        if (value instanceof Serializable) {
            intent.putExtra(key, (Serializable) value);
        }
        if (value instanceof Serializable[]) {
            intent.putExtra(key, (Serializable[]) value);
        }
        if (value instanceof Float) {
            intent.putExtra(key, (Float) value);
        }
        if (value instanceof Float[]) {
            intent.putExtra(key, (Float[]) value);
        }
        startActivity(intent);
    }


    public void startNewActivity(Class cls, Bundle bundle) {
        Intent intent = new Intent(this, cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
